package com.atguigu.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.atguigu.common.exception.BizCodeEnum;
import com.atguigu.member.exception.PhoneNumExistException;
import com.atguigu.member.exception.UserExistException;
import com.atguigu.member.feign.CouponFeignService;
import com.atguigu.member.vo.MemberLoginVo;
import com.atguigu.member.vo.MemberRegisterVo;
import com.atguigu.common.vo.SocialUser;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.member.entity.MemberEntity;
import com.atguigu.member.service.MemberService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;

import javax.annotation.Resource;


/**
 * 会员
 *
 * @author polarisink
 * @email polarisink@163.com
 * @date 2020-12-18 15:17:42
 */
@RestController
@RequestMapping("/member/member")
public class MemberController {
  @Resource
  private MemberService memberService;

  @Resource
  CouponFeignService couponFeignService;

  @PostMapping("/login")
  public R login(@RequestBody MemberLoginVo loginVo) {
    MemberEntity entity = memberService.login(loginVo);
    if (entity != null) {
      return R.ok().setData(entity);
    } else {
      return R.error(BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getCode(), BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getMsg());
    }
  }

  @PostMapping("/oauth2/login")
  public R oauthlogin(@RequestBody SocialUser socialUser) {
    MemberEntity entity = memberService.login(socialUser);
    if (entity != null) {
      return R.ok().setData(entity);
    } else {
      return R.error();
    }
  }

  /**
   * 注册会员
   *
   * @return
   */
  @PostMapping("/register")
  public R register(@RequestBody MemberRegisterVo registerVo) {
    try {
      memberService.register(registerVo);
    } catch (UserExistException userException) {
      return R.error(BizCodeEnum.USER_EXIST_EXCEPTION.getCode(), BizCodeEnum.USER_EXIST_EXCEPTION.getMsg());
    } catch (PhoneNumExistException phoneException) {
      return R.error(BizCodeEnum.PHONE_EXIST_EXCEPTION.getCode(), BizCodeEnum.PHONE_EXIST_EXCEPTION.getMsg());
    }
    return R.ok();
  }

  @RequestMapping("/coupons")
  public R test() {
    MemberEntity memberEntity = new MemberEntity();
    memberEntity.setNickname("张三");

    R membercoupons = couponFeignService.membercoupons();
    return R.ok().put("member", memberEntity).put("coupons", membercoupons.get("coupons"));
  }


  /**
   * 列表
   */
  @RequestMapping("/list")
  //@RequiresPermissions("member:member:list")
  public R list(@RequestParam Map<String, Object> params) {
    PageUtils page = memberService.queryPage(params);

    return R.ok().put("page", page);
  }


  /**
   * 信息
   */
  @RequestMapping("/info/{id}")
  //@RequiresPermissions("member:member:info")
  public R info(@PathVariable("id") Long id) {
    MemberEntity member = memberService.getById(id);

    return R.ok().put("member", member);
  }

  /**
   * 保存
   */
  @RequestMapping("/save")
  //@RequiresPermissions("member:member:save")
  public R save(@RequestBody MemberEntity member) {
    memberService.save(member);

    return R.ok();
  }

  /**
   * 修改
   */
  @RequestMapping("/update")
  //@RequiresPermissions("member:member:update")
  public R update(@RequestBody MemberEntity member) {
    memberService.updateById(member);

    return R.ok();
  }

  /**
   * 删除
   */
  @RequestMapping("/delete")
  //@RequiresPermissions("member:member:delete")
  public R delete(@RequestBody Long[] ids) {
    memberService.removeByIds(Arrays.asList(ids));

    return R.ok();
  }

}
